package com.qdlc.p2p.biz.log.score;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.constant.LogTemplateType;
import com.qdlc.p2p.common.constant.ScoreTemplateConstant;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.FreemarkerUtil;
import com.qdlc.p2p.dal.common.LogTemplateHelper;
import com.qdlc.p2p.dal.dao.RuleDao;
import com.qdlc.p2p.dal.dao.ScoreDao;
import com.qdlc.p2p.dal.dao.ScoreLogDao;
import com.qdlc.p2p.dal.dao.ScoreTypeDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.Score;
import com.qdlc.p2p.dal.dto.ScoreLog;
import com.qdlc.p2p.dal.dto.ScoreType;
import com.qdlc.p2p.dal.dto.User;

/**
 * 积分日志类
 */
public class BaseScoreLog extends ScoreLog implements ScoreLogEvent {

	private static final long serialVersionUID = 1L;

	protected UserDao userDao;

	protected RuleDao ruleDao;

	protected ScoreDao scoreDao;

	protected ScoreLogDao scoreLogDao;

	protected ScoreTypeDao scoreTypeDao;

	// 积分日志模板编码
	protected String logTemplateNid;

	public BaseScoreLog() {
		super();
		userDao = (UserDao) BeanUtil.getBean("userDao");
		ruleDao = (RuleDao) BeanUtil.getBean("ruleDao");
		scoreDao = (ScoreDao) BeanUtil.getBean("scoreDao");
		scoreLogDao = (ScoreLogDao) BeanUtil.getBean("scoreLogDao");
		scoreTypeDao = (ScoreTypeDao) BeanUtil.getBean("scoreTypeDao");
	}

	public BaseScoreLog(long userId, int score, String scoreTypeNid) {
		this();
		this.setScore(score);
		this.setUser(new User(userId));
		this.setType(scoreTypeNid);
	}

	/**
	 * 默认的事件执行
	 */
	@Override
	public void doEvent() {
		ScoreType type = scoreTypeDao.getScoreTypeByNid(this.getType());
		if (type != null
				&& type.getStatus() == ScoreTemplateConstant.SCORE_TYPE_ENABLE
				&& getScore() != 0) {
			// 修改积分
			this.modifyScore();
			// 添加积分日志
			this.addScoreLog();
			// 操作日志
			this.addOperateLog();
		}
	}

	/**
	 * 添加积分日志
	 */
	@Override
	public void addScoreLog() {
		// 积分日志模板
		Score item = scoreDao.findByUserId(getUser().getUserId());
		ScoreType type = scoreTypeDao.getScoreTypeByNid(this.getType());
		ScoreLog log = new ScoreLog(item);
		log.setRemark(this.getLogRemark()); // 获取日志备注
		log.setScoreTypeName(type.getName());// 类型类型名称
		log.setBehavior(type.getBehavior());// 表示获取还是消耗 1是获取，0是消耗
		log.setType(this.getLogType());// 积分类型
		log.setType(this.getType());// 积分类型
		log.setUser(new User(this.getUser().getUserId()));// 用户
		log.setScore(this.getScore());// 分数
		log.setAddTime(DateUtil.getNow());// 增加时间
		log.setAddIp(Global.getIP());// 设置IP
		scoreLogDao.save(log);
	}

	public String getLogRemark() {
		try {
			String logRemarkTemplate = LogTemplateHelper.getValue(LogTemplateType.SCORE_LOG, logTemplateNid);
			return FreemarkerUtil.renderTemplate(logRemarkTemplate, Global.getTransfer());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public String getLogType() {
		return LogTemplateHelper.getLogType(LogTemplateType.SCORE_LOG, logTemplateNid);
	}

	public void setLogTemplateNid(String logTemplateNid) {
		this.logTemplateNid = logTemplateNid;
	}

	@Override
	public void addOperateLog() {
	}

	@Override
	public void modifyScore() {
	}
}